A robust program is one that handles unexpected data and errors gracefully. It doesn't crash or behave unexpectedly.
Defensive Design & Maintainability
- Defensive design: writing code that anticipates and handles potential issues, especially from user input.
- Input validation: checks if user input is sensible and within a valid range (e.g., ensuring a number instead of text for age).
- Authentication: verifying a user’s identity before granting access to a system or data.
- Maintainability: writing code that is easy to understand and modify later.
- Meaningful variable and subroutine names.
- Comments for complex parts.
- Reusable modules and subroutines.
Testing & Errors
Testing is a vital part of the software development life cycle. It ensures your program works as intended.
- Iterative testing: testing as you go, after each small chunk of code.
- Final testing: comprehensive test of the complete program before release.
Use a variety of data types in testing:
- Normal data: valid data (e.g.,
15
for age).
- Boundary data: at the edges of valid range (e.g.,
0
or 100
for age).
- Erroneous data: invalid data (e.g.,
"hello"
for age).
Main types of errors:
- Syntax errors: breaking language rules (typo). Program won’t run.
- Logic errors: program runs but gives wrong results.
- Runtime errors: program crashes while running (e.g., divide by zero).
Trace tables help identify logic errors by tracking variable changes during execution.